package org.easymis.commons.generator.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class TestDb {
	public static  void  connectDataBase(String tableName,String databaseName)throws Exception{
		
		DBPropertiesUtil  dbutile = new DBPropertiesUtil();
		List<TableStructs>  ts_list = new ArrayList<TableStructs>();
		Connection con = null; //数据库的连接对象  
        Statement stmt = null;  //数据库的更新操作  
        Class.forName(dbutile.jdbcdrive); //1、使用CLASS 类加载驱动程序  
        con = DriverManager.getConnection(dbutile.jdbcurl,dbutile.jdbcuser,dbutile.jdbcpassword); //2、连接数据库 操作 
        stmt = con.createStatement(); //3、Statement 接口需要通过Connection 接口进行实例化操作  		  
		  while(true){
			  
			  String sql1="INSERT INTO `test`.`test1` (`name`) VALUES ( '"+UUID.randomUUID().toString()+"')";
			  //stmt.execute(sql1);
			  String sql2="INSERT INTO `test`.`test2` (`id`, `name`) VALUES ('"+UUID.randomUUID().toString()+"', '2')";
			  stmt.execute(sql2);
			  String sql3="INSERT INTO `test`.`test3` (`id2`, `name`) VALUES ('2', '"+UUID.randomUUID().toString()+"')";
			 // stmt.execute(sql3);
  		  }
	}
	public static void  query(String tableName,String databaseName,String id)throws Exception{
		
		DBPropertiesUtil  dbutile = new DBPropertiesUtil();
		Connection con = null; //数据库的连接对象  
        Statement stmt = null;  //数据库的更新操作  
        Class.forName(dbutile.jdbcdrive); //1、使用CLASS 类加载驱动程序  
        con = DriverManager.getConnection(dbutile.jdbcurl,dbutile.jdbcuser,dbutile.jdbcpassword); //2、连接数据库 操作 
        stmt = con.createStatement(); //3、Statement 接口需要通过Connection 接口进行实例化操作
		
		  String sql = "  select * from "+databaseName+"."+tableName+" where id="+id;
		  ResultSet rs = stmt.executeQuery(sql);
		  Date a,b;
		  a = new Date();		 
		  while(rs.next()){
			  System.out.println("test1.name:"+rs.getObject(2).toString());
  		  }
		  b = new Date();
		  System.out.println("单主键数字用时："+getDistanceTime(a,b));
        rs.close();  
        con.close(); // 4、关闭数据库连接  
	}
	public static void  query2(String tableName,String databaseName,String id)throws Exception{
		
		DBPropertiesUtil  dbutile = new DBPropertiesUtil();
		Connection con = null; //数据库的连接对象  
        Statement stmt = null;  //数据库的更新操作  
        Class.forName(dbutile.jdbcdrive); //1、使用CLASS 类加载驱动程序  
        con = DriverManager.getConnection(dbutile.jdbcurl,dbutile.jdbcuser,dbutile.jdbcpassword); //2、连接数据库 操作 
        stmt = con.createStatement(); //3、Statement 接口需要通过Connection 接口进行实例化操作
		
		  String sql = "  select * from "+databaseName+"."+tableName+" where id='"+id+"'";
		  ResultSet rs = stmt.executeQuery(sql);
		  Date a,b;
		  a = new Date();		 
		  while(rs.next()){
			  System.out.println("test2.name"+rs.getObject(2).toString());
  		  }
		  b = new Date();
		  System.out.println("单主键字母用时："+getDistanceTime(a,b));
        rs.close();  
        con.close(); // 4、关闭数据库连接  
	}
	public static void  query3(String tableName,String databaseName,String id)throws Exception{
		
		DBPropertiesUtil  dbutile = new DBPropertiesUtil();
		Connection con = null; //数据库的连接对象  
        Statement stmt = null;  //数据库的更新操作  
        Class.forName(dbutile.jdbcdrive); //1、使用CLASS 类加载驱动程序  
        con = DriverManager.getConnection(dbutile.jdbcurl,dbutile.jdbcuser,dbutile.jdbcpassword); //2、连接数据库 操作 
        stmt = con.createStatement(); //3、Statement 接口需要通过Connection 接口进行实例化操作
		
		  String sql = "  select * from "+databaseName+"."+tableName+" where id="+id;
		  ResultSet rs = stmt.executeQuery(sql);
		  Date a,b;
		  a = new Date();		 
		  while(rs.next()){
			  System.out.println("test3.name"+rs.getObject(2).toString());
  		  }
		  b = new Date();
		  System.out.println("多主键字母用时："+getDistanceTime(a,b));
        rs.close();  
        con.close(); // 4、关闭数据库连接  
	}
	  public static String getDistanceTime(Date one, Date two) throws ParseException {  
	        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 	        
	        long day = 0;  
	        long hour = 0;  
	        long min = 0;  
	        long sec = 0;  
	        long ms = 0;  
	        long time1 = one.getTime();  
			long time2 = two.getTime();  
			long diff ;  
			if(time1 < time2) {  
			    diff = time2 - time1;  
			} else {  
			    diff = time1 - time2;  
			}  
			day = diff / (24 * 60 * 60 * 1000);  
			hour = (diff / (60 * 60 * 1000) - day * 24);  
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);  
			sec = (diff/1000-day*24*60*60-hour*60*60-min*60);  
			//ms = (diff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - sec * 1000);    
	        return day + "天" + hour + "小时" + min + "分" + sec + "秒" + ms + "毫秒";  
	    }  
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		TestDb.connectDataBase("", "");
		//TestDb.query("test1", "test", "305154");
		//TestDb.query2("test2", "test", "00041f54-8b3b-4b08-bda1-5925528f3a77");
		//TestDb.query3("test3", "test", "305154");
	}

}
